﻿#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<vector>
#include<type_traits>
#include<map>
#include<list>
using namespace std;
void func(int& x)
{
	cout << "void func(int& x)" << endl;
}
void func(int&& x)
{
	cout << "void func(int&& x)" << endl;
}
void func(const int& x)
{
	cout << "void func(const int& x)" << endl;
}
void func(const int&& x)
{
	cout << "void func(const int&& x)" << endl;
}
auto getComplexType() -> std::map<std::string, std::vector<int>> {
	// ...
	return std::map<std::string, std::vector<int>>();
}
template<class T,class U>
auto getvalue(T a, U b) -> decltype(a+b)
{
	return a + b;
}

void test()
{
	int a = 0;
	int& b = a;
	const int& h = b;
	decltype(a) d;
	decltype(b) e =d;
	decltype(h) j= e;
	int* p1 = &a;
	decltype(p1) p2 = nullptr;
	decltype(*p1) p3 = a;
	cout << typeid(p3).name() << endl;
	decltype((a)) p4 =a;
	cout << typeid(p4).name() << endl;


	

}
template <typename T>
class A
{
public:
	void func( T& container)
	{
		_it = container.begin();
	}
private:
	// 这⾥不确定是	iterator	还是	const_iterator，也不能使⽤	auto
	//	typename T::iterator _it;
	// 使⽤	decltype	推导就可以很好的解决问题

		decltype(T().begin()) _it;
};
void test1()

{
	int i = 0;
	int& ri = i;
	const int ci = 42; // 顶层	const
		int* const p1 = &i; // 顶层	const
		const int* p2 = &ci; // 底层const
		auto j = ri; // j类型为	int
		decltype(auto) j1 = ri; // j1	类型为	int&
		++j1;
	auto r1 = ci; // r1	类型为		int		，忽略掉顶层		const
		decltype(auto) rr1 = ci; // rr1	类型为	const int
		r1++;
	//rr1++;
		const int*const c2 = &i;
		decltype(auto) rr2 = c2;
		cout << typeid(rr2).name() << endl;
		
}
void test3()
{
	const vector<int> v1;
	A<const vector<int>> obj1;
	obj1.func(v1);
	vector<int> v2;
	A<vector<int>> obj2;
	obj2.func(v2);
}
template< class Iter>
auto Func(Iter it1, Iter it2)->decltype(*it1)
{
	auto& x = *it1;
	++it1;
	while (it1 != it2)
	{
		x += *it1;
		++it1;
	}
	return x;
}
template <class T>
using Map = map<T, int>;
using fun12 = void(*)();
void test5()
{
	Map<string> map;
	fun12 f3 = test3;
	f3();

}
enum class t1:uint16_t
{
	red
	,blue
};
enum class t2:uint32_t
{
	red
	,blue
};
void test6()
{
	t1 a = static_cast<t1>(t2::red);
	t2 a1 = t2::red;
	t1 a2 = t1::red;
}
void test4()
{
	vector<int> v = { 1,2,3 };
	list<string> lt = { "111","222","333" };
	// 这⾥⽆法调⽤上⾯的函数，因为函数模板只能通过实参推导模板类型，⽆法推导	
	auto ret1 = Func(v.begin(), v.end());
	auto ret2 = Func(lt.begin(), lt.end());
	// 显⽰实例化能解决问题，但是调⽤就很⿇烦

		//auto ret1 = Func<decltype(*v.begin())>(v.begin(), v.end());
		// auto ret2 = Func<decltype(*lt.begin())>(lt.begin(), lt.end());
		cout << ret1 <<" "<<(*(v.begin()))<< endl;
	cout << ret2 <<" "<<(*(lt.begin()))<< endl;
}
template<typename T>
void process(T value) {
	static_assert(std::is_integral<T>::value, "T must be an integral type");
	// 函数实现	...
}
void test7()
{
	vector<int> b;
	int a = 1;
	process(a);
	constexpr int buffer_size = 1024;
	static_assert(buffer_size > 0, "Buffer size must be positive");
	static_assert(buffer_size % 4 == 0, "Buffer size must be divisible by 4"); 
//	static_assert(sizeof(b)==4, "dadadad");

}
void test8()
{
	tuple<string, int, double, char> s1("dadadad", 1, 1.0, 'a');
	cout << get<1>(s1) << endl;
	cout << get<0>(s1) << endl;
	cout << get<3>(s1) << endl;
	tuple s2("dadadad", 1, 1.0, 'a');
	auto t2 = make_tuple("dadadad", 1, 1.0, 'a');

}
int main()
{
	test8();
	 const vector< int> v;
	A<const vector<int>> hhh;
	hhh.func(v);
	int x = 10;
	const int cx = 20;
	auto& rx1 = x;   // int&
	auto& rx2 = cx;  // const int&
	func(rx1);
	func(rx2);
	const auto& rx3 = x;   // const int&
	const auto& rx4 = cx;  // const int&
	func(rx3);
	func(rx4);
	// 万能引⽤

		auto&& rx5 = x;   // int&
	auto&& rx6 = cx;  // const int&
	func(rx5);
	func(rx6);
	auto&& rx7 = move(x);   // int&&
	//rx7++;
	auto&& rx8 = move(cx);  // const int&&
	//rx8++;
	func(forward<int>(rx7));
	func(forward<const int>(rx8));
	func(static_cast<const int&&>(1));
	return 0;
}